package com.eboss2.sbs.adapter;

import android.util.SparseArray;
import android.view.View;

import com.eboss2.sbs.myapplication.MyApplication;


/**
 * 通用ViewHolder
 * 
 * @author yang
 * @since 2016-8-29 下午6:49:14
 */
public class CommonViewHolder {
	private View mLayout;
	private SparseArray<View> mViews;

	private CommonViewHolder(View layout) {
		this.mLayout = layout;
		mViews = new SparseArray<View>();
	}

	/**
	 * 获取ViewHolder实例
	 * 
	 * @param layout
	 *            布局
	 * @param layoutId
	 *            布局id
	 */
	public static CommonViewHolder getInstance(View layout, int layoutId) {
		CommonViewHolder viewHolder;
		if (layout == null) {
			layout = View.inflate(MyApplication.applicationContext, layoutId,
					null);
			viewHolder = new CommonViewHolder(layout);
			layout.setTag(viewHolder);
		} else {
			viewHolder = (CommonViewHolder) layout.getTag();
		}
		return viewHolder;
	}

	/**
	 * 获取布局控件
	 * 
	 * @param viewId
	 *            控件id
	 * @return
	 */
	public View getView(int viewId) {
		View view = mViews.get(viewId);
		if (view == null) {
			view = mLayout.findViewById(viewId);
			mViews.put(viewId, view);
		}
		return view;
	}

	/** 获取布局 */
	public View getLayout() {
		return this.mLayout;
	}
}
